<?php

	$err_reported = NULL;

	function display_error($error = '', $swap = '', $native = FALSE) { 
		GLOBAL $err_reported;
		if (is_array($error)) {
			$e = '';
			foreach($error as $v) {
				$e .= $v . ' ';
			}
			$err_reported = 'error: ' . $e;
		} else {
			$err_reported = 'error: ' . $error;
		}
	}

	require_once('../includes/database-connector.php');
	
	$_callback = $_REQUEST['callback'];
	$_action = $_REQUEST['act'];

	define('SY_DEBUG', FALSE);

	// init
	$result = TRUE;

	if (isset($_action))
	{
		switch ($_action) 
		{
			case 'q':
				$qs = $_REQUEST['qs'];
				$c = get_database_connector_instance( /* default */ );
				
				$q = $c->query( $qs );

				if ($q) {
					$data = $q->result_array();
					$result = TRUE;
				} else {
					$data = $q;
					$err = $err_reported;
					$result = FALSE;
				}
			break;
			default:
				$err = 'unknown \'act\' value: '.$_action;
				$result = FALSE;
			break;
		}
	}
	else
	{
		$err = 'undefined \'act\' value';
		$result = FALSE;
	}

	// enclosure
	echo $_callback.'('.json_encode(array(
			'result' => $result,
			'data' => $data,
			'err' => $err
		)).');';
?>
